package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.android.uu.view.ActionSheetDialog;
import com.android.uu.view.ActionSheetDialog.OnSheetItemClickListener;
import com.android.uu.view.ActionSheetDialog.SheetItemColor;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.ClientInfor;
import com.hemaapp.yjnh.bean.EventBusConfig;
import com.hemaapp.yjnh.bean.EventBusMsg;
import com.hemaapp.yjnh.bean.User;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import xtom.frame.util.XtomToastUtil;

/**
 * 我的钱包
 */
public class MyWalletActivity extends BaseActivity implements OnClickListener {

    @Bind(R.id.title_left_btn)
    ImageButton titleLeftBtn;
    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.tv_rate)
    TextView tvRate;
    @Bind(R.id.tv_yesterday_earnings)
    TextView tvYesterdayEarnings;
    @Bind(R.id.tv_history_earnings)
    TextView tvHistoryEarnings;
    @Bind(R.id.tv_total_fee)
    TextView tvTotalFee;

    private User user;
    private ClientInfor infor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_wallet);
        ButterKnife.bind(this);
        user = BaseApplication.getInstance().getUser();
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        getClientInfo();
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private void getClientInfo() {
        if (user != null) {
            getNetWorker().clientInfor(user.getToken());
        }
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case ACCOUNT_GET:
                showProgressDialog("请稍候");
                break;
            default:
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case ACCOUNT_GET:
                cancelProgressDialog();
                break;
            default:
                break;
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case ACCOUNT_GET:
                HemaArrayResult<ClientInfor> cResult = (HemaArrayResult<ClientInfor>) baseResult;
                infor = cResult.getObjects().get(0);
//                tvRate.setText(uuUtils.formatAfterDot2(infor.getRebat_rate()));
                if (!isNull(infor.getRebat_rate())) {
                    StringBuffer rate = new StringBuffer(infor.getRebat_rate());
                    while (rate.substring(rate.length() - 1, rate.length()).equals("0")) {
                        rate.deleteCharAt(rate.length() - 1);
                    }
                    tvRate.setText(rate);
                }else {
                    tvRate.setText("0");
                }
                String fee = uuUtils.formatAfterDot2(infor.getFeeaccount());
                tvTotalFee.setText("¥" + fee);
                user.setFeeaccount(fee);
                tvYesterdayEarnings.setText(uuUtils.formatAfterDot2(infor.getYestoday_rebat()));
                tvHistoryEarnings.setText(uuUtils.formatAfterDot2(infor.getTotal_rebat()));
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case ACCOUNT_GET:
                XtomToastUtil.showShortToast(mContext, "获取信息失败"+baseResult.getMsg());
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case ACCOUNT_GET:
                XtomToastUtil.showShortToast(mContext, "获取信息失败,请稍后再试");
                break;
        }
    }

    @Override
    protected void findView() {
    }

    @Override
    protected void getExras() {
    }

    @Override
    protected void setListener() {
        titleText.setText("我的钱包");
        titleRightBtn.setVisibility(View.GONE);
    }

    @OnClick({R.id.title_left_btn, R.id.tv_charge_history, R.id.tv_withdraw_history,
            R.id.tv_trading_history, R.id.layout_charge, R.id.layout_withdraw,
            R.id.tv_rate_title, R.id.layout_history})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.tv_rate_title:
                Intent itInfo = new Intent(mContext, WebviewActivity.class);
                itInfo.putExtra("type", 17);
                itInfo.putExtra("parm", "rebat_intro");
                startActivity(itInfo);
                break;
            case R.id.layout_history:
                Intent itRate = new Intent(mContext, EarningDetailsActivity.class);
                itRate.putExtra("total", uuUtils.formatAfterDot2(infor.getTotal_rebat()));
                startActivity(itRate);
                break;
            case R.id.tv_charge_history://充值记录
                Intent itCZRecord = new Intent(mContext, ChargeListActivity.class);
                startActivity(itCZRecord);
                break;
            case R.id.tv_withdraw_history://提现记录
                Intent itTXRecord = new Intent(mContext, CashListActivity.class);
                startActivity(itTXRecord);
                break;
            case R.id.tv_trading_history://交易记录
                Intent itTradeRecord = new Intent(mContext, TradeListActivity.class);
                startActivity(itTradeRecord);
                break;
            case R.id.layout_charge://充值
                Intent itCharge = new Intent(mContext, ChargeMoneyActivity.class);
                itCharge.putExtra("type", 1);
                startActivity(itCharge);
                break;
            case R.id.layout_withdraw://提现
                new ActionSheetDialog(mContext)
                        .builder()
                        .setCancelable(true)
                        .setCanceledOnTouchOutside(true)
                        .addSheetItem("支付宝", SheetItemColor.Black,
                                new OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        Intent itAlipay = null;
                                        if (!isNull(user.getAlipay())) {//已设置支付宝
                                            itAlipay = new Intent(mContext, Cash2AlipayActivity.class);
                                        } else {
                                            itAlipay = new Intent(mContext, AlipayAccountEditActivity.class);
                                            itAlipay.putExtra("needtoCashAdd", true);// 如果是首次设置支付宝，跳转至提现页面
                                        }
                                        startActivity(itAlipay);
                                    }
                                })
                        .addSheetItem("银行卡", SheetItemColor.Black,
                                new OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        Intent itUnipay = null;
                                        if (!isNull(user.getBankcard())
                                                && !isNull(user.getBankuser())
                                                && !isNull(user.getBankname())
                                                && !isNull(user.getBankaddress())) {
                                            itUnipay = new Intent(mContext, Cash2BankActivity.class);
                                        } else {
                                            itUnipay = new Intent(mContext, BankEditActivity.class);
                                            itUnipay.putExtra("needtoCashAdd", true);// 如果是首次设置银行卡，跳转至提现页面
                                        }
                                        startActivity(itUnipay);
                                    }
                                })
                        .setCancelColor(getResources().getColor(R.color.text_dialog_cancel)).show();
                break;
        }
    }

    @Subscribe
    public void onEventMainThread(EventBusMsg msg) {
        if (msg.getType() == EventBusConfig.UPDATE_INFO) {
            getClientInfo();
        }
    }
}